/*
 * Copyright 2020 wangruiCoder owner
 */

package org.disk.frame.logger.listener;

import org.disk.frame.logger.client.LogClient;
import org.disk.frame.logger.config.DiskFrameLoggerConfiguration;
import org.disk.frame.logger.event.HttpLog;
import org.disk.frame.plugin.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.annotation.Async;

/**
 * 类描述
 * <p>详细描述</p>
 *
 * @author kyrie 2021/4/23 9:59 上午
 * @since jdk1.8
 */
public class HttpLogListener implements ApplicationListener<HttpLog>, Ordered {
    private static final Logger logger = LoggerFactory.getLogger(HttpLogListener.class);

    @Autowired
    private LogClient logClient;
    @Autowired
    private DiskFrameLoggerConfiguration loggerConfiguration;

    @Async
    @Override
    public void onApplicationEvent(@NonNull HttpLog event) {
        if (logger.isDebugEnabled()){
            logger.debug("Log body :{}", JsonUtils.toJson(event));
        }
        logClient.send(loggerConfiguration.getQueueName(), JsonUtils.toJson(event));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
